/**
 * Copyright 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: jacoby (Jacoby Thwaites)
 *
 * Javascript behaviour implementations for the TXM manager UI.
 */

$(document).ready(function() {

  /*
   * Add event handler on collapse button.
   */
  $("[behaviour='collapse']").click(
    function(e) {
      $("#collapse-group").addClass("g-collapsed");
    });

  /*
   * Add event handler on expand button.
   */
  $("[behaviour='expand']").click(
    function(e) {
      $("#collapse-group").removeClass("g-collapsed");
    });

  /*
   * Delete links invoke the modal delete dialog on the name specified
   * in the args attribute on the DOM element generated by XSL.
   */
  $("[behaviour='delete']").click(
    function(e) {
      var name = $(this).attr("args");
      $("#deleteNameElem").attr("value", name);
      $("body").addClass("modal");
      $("#deleteDialog").addClass("active");
    });
    
  /*
   * Upload links invoke the upload dialog.
   */
  $("[behaviour='upload']").click(
    function(e) {
      $("body").addClass("modal");
      $("#uploadDialog").addClass("active");
    });

  /*
   * New field modal appears on click.
   */
  $("[behaviour='newField']").click(
    function(e) {
      $("body").addClass("modal");
      $("#fieldDialog").addClass("active");
    });

  /*
   *  New folder modal appears on click.
   */
  $("[behaviour='newFolder']").click(
    function(e) {
      $("body").addClass("modal");
      $("#folderDialog").addClass("active");
    });

  /*
   * New op modal appears on click, with the composite option checked
   * if appropriate.
   */
  $("[behaviour='newOp']").click(
    function(e) {
      $("body").addClass("modal");
      $("#opDialog").addClass("active");

      var composite = $(this).attr("args");
      if (composite == "composite") {
        $("#isComposite").attr("checked", "checked");
      }
      else {
        $("#isComposite").removeAttr("checked");
      }
    });

  /*
   * New version modal appears on click. The form's hidden field is
   * set to the particular op ref so it gets passed to the server.
   */
  $("[behaviour='newVersion']").click(
    function(e) {
      var opRef = "{op," + $(this).attr("args") + "}";
      $("#opRefInputElem").attr("value", opRef);
      $("body").addClass("modal");
      $("#versionDialog").addClass("active");
    });

  /*
   * Cancel modal form.
   */
  $("[behaviour='cancelModal']").click(
    function(e) {
      $("body").removeClass("modal");
      $(this).parents(".modal-dialog").removeClass("active");
    });

  /*
   * Field rollover causes highlight of field occurrences.
   */
  $("[behaviour='highlightField']").mouseover(
    function(e) {
      var genId = $(this).attr("args");
      $("body").addClass(genId + "-active");
    });

  /*
   * Field rollout ceases highlight of field occurrences.
   */
  $("[behaviour='highlightField']").mouseout(
    function(e) {
      var genId = $(this).attr("args");
      $("body").removeClass(genId + "-active");
    })
});
